package icu.qx.pbs.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import icu.qx.pbs.domain.GbsGood;
import icu.qx.pbs.domain.PbsProuteProcedure;
import icu.qx.pbs.domain.param.ProcedureRouteParam;
import icu.qx.pbs.domain.param.ProcedureRouteUpParam;
import icu.qx.pbs.feign.GbsGoodFeign;
import icu.qx.pbs.service.IPbsProuteProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pbs.domain.PbsProcedureRoute;
import icu.qx.pbs.service.IPbsProcedureRouteService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工艺路线Controller
 *
 * @author yong
 * @date 2024-06-15
 */
@RestController
@RequestMapping("/pbs/procedureRoute")
public class PbsProcedureRouteController extends BaseController {
    @Autowired
    private IPbsProcedureRouteService pbsProcedureRouteService;

    @Autowired
    private IPbsProuteProcedureService pbsProuteProcedureService;

    @Autowired
    private GbsGoodFeign gbsGoodFeign;

    /**
     * 查询工艺路线列表
     */
    @RequiresPermissions("pbs:procedureRoute:list")
    @GetMapping("/list")
    public TableDataInfo list(PbsProcedureRoute pbsProcedureRoute) {
        startPage();
        List<PbsProcedureRoute> list = pbsProcedureRouteService.selectPbsProcedureRouteList(pbsProcedureRoute);
        return getDataTable(list);
    }

    /**
     * 导出工艺路线列表
     */
    @RequiresPermissions("pbs:procedureRoute:export")
    @Log(title = "工艺路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PbsProcedureRoute pbsProcedureRoute) {
        List<PbsProcedureRoute> list = pbsProcedureRouteService.selectPbsProcedureRouteList(pbsProcedureRoute);
        ExcelUtil<PbsProcedureRoute> util = new ExcelUtil<PbsProcedureRoute>(PbsProcedureRoute.class);
        util.exportExcel(response, list, "工艺路线数据");
    }

    /**
     * 获取工艺路线详细信息
     */
    @RequiresPermissions("pbs:procedureRoute:query")
    @GetMapping(value = "/{prouteCode}")
    public AjaxResult getInfo(@PathVariable("prouteCode") String prouteCode) {
        AjaxResult success = success();
        PbsProcedureRoute pbsProcedureRoute = pbsProcedureRouteService.selectPbsProcedureRouteByProuteCode(prouteCode);
        PbsProuteProcedure pbsProuteProcedure = new PbsProuteProcedure();
        pbsProuteProcedure.setPpProuteCode(pbsProcedureRoute.getProuteCode());
        List<PbsProuteProcedure> pbsProuteProcedures = pbsProuteProcedureService.selectPbsProuteProcedureList(pbsProuteProcedure);
        AjaxResult routeGoods = gbsGoodFeign.getInfoByRoute(prouteCode);
        System.err.println("res " + routeGoods);
        success.put("data", pbsProcedureRoute);
        success.put("pbsProuteProcedures", pbsProuteProcedures);
        success.put("goods", routeGoods.get("data"));
        return success;
    }

    /**
     * 新增工艺路线
     */
    @RequiresPermissions("pbs:procedureRoute:add")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcedureRouteParam procedureRouteParam) {
        return toAjax(pbsProcedureRouteService.insertPbsProcedureRoute(procedureRouteParam));
    }

    /**
     * 修改工艺路线
     */
    @RequiresPermissions("pbs:procedureRoute:edit")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcedureRouteUpParam procedureRouteUpParam) {
        System.err.println("udpate \n" + procedureRouteUpParam);
        return toAjax(pbsProcedureRouteService.updatePbsProcedureRoute(procedureRouteUpParam));
    }

    /**
     * 删除工艺路线
     */
    @RequiresPermissions("pbs:procedureRoute:remove")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prouteCodes}")
    public AjaxResult remove(@PathVariable String[] prouteCodes) {
        return toAjax(pbsProcedureRouteService.deletePbsProcedureRouteByProuteCodes(prouteCodes));
    }
}
